package servlet;

import dao.ProductDAO;
import dao.UserDAO;
import dao.UserDAO1;
import entity.PageBean;
import entity.Product;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 功能：
 * 作者：dy
 * 日期：2024/7/15 15:47
 */
@WebServlet(name="userList", value = "/user/list")
public class UserListAllServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 接受pageNum=1 pageSize=10
        int pageNum = 1;
        int pageSize = 10;
        //2. 将pageNum pageSize === skip 和limit
        int skip = (pageNum-1)*pageSize;
        int limit = pageSize;
        //3. 调用productDAO查询数据 list
        UserDAO1 userDAO1 = new UserDAO1();
        List<User> list = userDAO1.findPage(skip, limit);
        //获得products数据总条数
        long total = userDAO1.count();
        //4. 将total list pageNum pageSize 封装--->PageBean 传给List.jsp
        PageBean<User> page1 = new PageBean<>();
        page1.setPageNum(pageNum);
        page1.setPageSize(pageSize);
        page1.setList(list);
        page1.setTotal(total);
        page1.setPageTotal(total,pageSize);
        req.setAttribute("page1",page1);
        //5. 跳转
        req.getRequestDispatcher("/user/List.jsp").forward(req,resp);
    }
}
